/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: agent-java
 * Description: Agent written in java language
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.agent.db;

import static ru.ffind.agent.db.DbTools.getRsByte;
import static ru.ffind.agent.db.DbTools.getRsInteger;
import static ru.ffind.agent.db.DbTools.getRsLong;
import static ru.ffind.agent.db.DbTools.getRsShort;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import ru.ffind.common.model.Directory;
import ru.ffind.common.model.Entry;

import com.google.common.collect.ImmutableList;

/**
 * Implementation for the directory stored in the database
 *
 * @author Rage Steel
 */
public class DbDirectory extends AbstractDbEntry implements Directory {

	private Integer dirId;
	private Integer parDirId;
	private Short hostId;
	private Byte dirFlags;
	private String dirLinkPath;
	private Integer filesCount;
	private Integer dirsCount;
	private Long totalSize;
	
	public DbDirectory(
			Integer dirId, Integer parDirId, Short hostId, String dirName, 
			Byte dirFlags, String dirLinkPath, 
			Integer filesCount, Integer dirsCount, Long totalSize,
			Date dirDatetime, Date appDate) {
		
		super(dirName, dirDatetime, appDate);
		
		this.dirId = dirId;
		this.parDirId = parDirId;
		this.hostId = hostId;
		this.dirFlags = dirFlags;
		this.dirLinkPath = dirLinkPath;
		this.filesCount = filesCount;
		this.dirsCount = dirsCount;
		this.totalSize = totalSize;
	}
	
	public final static RowMapper<DbDirectory> ROW_MAPPER = new RowMapper<DbDirectory>() {

		@Override
		public DbDirectory mapRow(ResultSet rs, int rowNum) throws SQLException {
			return new DbDirectory(getRsInteger(rs, "dir_id"), getRsInteger(rs, "par_dir_id"), getRsShort(rs, "host_id"), rs.getString("dir_name"), 
				getRsByte(rs, "dir_flags"), rs.getString("dir_link_path"),
				getRsInteger(rs, "files_count"), getRsInteger(rs, "dirs_count"), getRsLong(rs, "total_size"),
				rs.getTimestamp("dir_datetime"), rs.getDate("app_date"));
		}
		
	};
	

	@Override
	public long getCount() {
		return 1;
	}

	private ImmutableList<Entry> entries = null;
	
	public void setEntries(ImmutableList<Entry> entries) {
		this.entries = entries;
	}
	
	@Override
	public ImmutableList<Entry> getEntries() {
		if (null == entries) {
			throw new UnsupportedOperationException("DbDirectory is a placeholder");
		}
		return entries;
	}

	@Override
	public boolean isError() {
		// FIXME Flags?
		return false;
	}

	// Getters
	
	public Integer getDirId() {
		return dirId;
	}

	public Integer getParDirId() {
		return parDirId;
	}

	public Short getHostId() {
		return hostId;
	}

	public Byte getDirFlags() {
		return dirFlags;
	}

	public String getDirLinkPath() {
		return dirLinkPath;
	}

	public Integer getFilesCount() {
		return filesCount;
	}

	public Integer getDirsCount() {
		return dirsCount;
	}

	public Long getTotalSize() {
		return totalSize;
	}
	
	// Setters

	public void setParDirId(Integer parDirId) {
		this.parDirId = parDirId;
	}

	public void setHostId(Short hostId) {
		this.hostId = hostId;
	}

	public void setDirFlags(Byte dirFlags) {
		this.dirFlags = dirFlags;
	}

	public void setDirLinkPath(String dirLinkPath) {
		this.dirLinkPath = dirLinkPath;
	}

	public void setFilesCount(Integer filesCount) {
		this.filesCount = filesCount;
	}

	public void setDirsCount(Integer dirsCount) {
		this.dirsCount = dirsCount;
	}

	public void setTotalSize(Long totalSize) {
		this.totalSize = totalSize;
	}

	/*
	@Override
	public boolean equals(Object obj) {
		return AbstractEntry.dirEquals(this, obj);
	}
	*/
	
}
